
cc.Class({
    extends: cc.Component,

    properties: {
       Type:1,
    },
    // LIFE-CYCLE CALLBACKS:
    // onLoad () {},
    start () {
        this.nodeClone = cc.instantiate(this.node);
        this.game = cc.find("Canvas").getComponent('Home');
        this.drag();
        this.place = false;
        this.placeNode = null;//放置的汉堡节点
    },
    drag(){
        this.parentInit = null;
        this.node.on('touchstart',function(event){
            if(!this.game.singleDrag) return;
            this.game.singleDrag = false;
            if(this.parentInit) return;
            this.parentInit = this.node.parent;
            this.node.parent = cc.find('Canvas');
            var po = cc.find('Canvas').convertToNodeSpaceAR(this.parentInit.convertToWorldSpaceAR(cc.p(this.node.x,this.node.y)));
            this.node.setPosition(po);
            this.node.opacity = 255;
            this.node.zIndex=99;
        },this);
        this.node.on('touchmove',function(event){
            if(!this.parentInit) return;
            var touches = event.getTouches();
            this.node.x += touches[0].getDelta().x;
            this.node.y += touches[0].getDelta().y;
        },this);
        this.node.on('touchend',function(event){
            if(!this.parentInit) return;
            if(this.place){
                this.placeNode.getComponent('hamburger').placeVegetable(this.Type);
                this.addNew();
                this.node.destroy();
            }else{
                this.node.opacity = 0;
                this.node.parent = this.parentInit;
                this.parentInit=null;
                this.node.setPosition(0,0);
            }
            this.game.singleDrag = true;
        },this);
        this.node.on('touchcancel',function(event){
            if(!this.parentInit) return;
            if(this.place){
                this.placeNode.getComponent('hamburger').placeVegetable(this.Type);
                this.addNew();
                this.node.destroy();
            }else{
                if(!this.parentInit) return;
                this.node.opacity = 0;
                this.node.parent = this.parentInit;
                this.parentInit=null;
                this.node.setPosition(0,0);
            }
            this.game.singleDrag = true;
        },this)
    },
    onCollisionEnter: function (other, self) {
        this.place = true;
        this.placeNode = other.node;
    },
    onCollisionExit: function (other, self) {
        this.place = false;
    },
    addNew(){
        console.log('重新生成一个材料');
        this.parentInit && this.parentInit.addChild(this.nodeClone);
    },
    // update (dt) {},
});
